<?php
namespace Shopmanage\Controller;
use Think\Controller;
class EditController extends EventController {
	
	public function front_cfg(){
		$eid = I("eid");
		$mob = I("mob");
		$cur = I("cur");
		$skin = I("skin");
		
		if($skin=="mob" && !$mob){
			$this->redirect('Event/step4_mob', array('eid' => $eid));
			die();
		}
		
		if($skin=="pc" && !$cur){
			$this->redirect('Event/step4_pc', array('eid' => $eid));
			die();
		}
		
		$EVENT = D("Events");
		$EVENT->sid=$this->sid;
		$EVENT->eid=$eid;
		if($skin=="mob"){
			$cfg = $EVENT->getMobCfg();
		}else{
			$cfg = $EVENT->getFrontCfg();
			$frount_default = $EVENT->frount_default;
			$gift_default = $EVENT->gift_default;

			$GFT = M("EventsGifts");
			$gifts = $GFT->where("`sid`=".$this->sid." AND `status`<>9 AND `event_id`=".$eid)->getField("id,type,level,name,status");
			$gifts["0"]["name"]="不中奖";
		}
		
		if($mob=="mob_dices"||$mob=="mob_wheel"){
			$GFT=M("EventsGifts");
			$gft = $GFT->where(Array("event_id"=>$eid))->getField("id,name");
		}
		
		
		$info["type"]="warning";
		$info["msg"]="亲，在进行操作时请先确认对店铺没有影响。";

		$this->assign("gft",$gft);
		$this->assign("eid",$eid);
		$this->assign("info",$info);
		$this->assign("issetted",$EVENT->issetted);
		$this->assign("sid",$this->sid);
		$this->assign("bg_default",C("DEFAULT_FRONT_PIC_URL")[$cfg["frontcfg"]["curtool"]]);
		$this->assign("frount_default",$frount_default);
		$this->assign("gift_default",$gift_default);
		$this->assign("cfg",$cfg);
		$this->assign("event",$cfg);
		$this->assign("gifts",$gifts);
		$this->assign("title","绑定奖品");
		$this->assign("current","waiguan");
		if($skin=="mob"){
			$this->assign("curtool",$cfg["mobtool"]);
			$this->display("Event:step4_".$cfg["mobtool"]);
		}else{
			$this->assign("curtool",$cfg["frontcfg"]["curtool"]);
			$this->display("Event:step4_pc_cfg");
		}
	}
	public function clearcfg(){
		$eid = I("eid");
		M("Events")->where(Array("sid"=>$this->sid,"id"=>$eid))->save(Array("front_mob_cfg"=>""));
		$this->redirect($_SERVER['HTTP_REFERER']);
	}
	public function setting_save(){
		$start_time = I("post.start_time");
		$end_time = I("post.end_time");
		$eid = I("post.eid");
		if(!$eid){
			$this->success("请选择要修改的活动！",U("Shopmanage/Event/Index"));
			die();
		}
		$EVENT = D("Events");
		$EVENT->c = $this->c;
		$EVENT->sid = $this->sid;
		$EVENT->start_time = $start_time;
		$EVENT->end_time = $end_time;
		$EVENT->except_eid = $eid;
		$time_duble = $EVENT->check_time();
		// 检查时间
		if($time_duble){
			$this->success("时间范围重叠，请重新选择时间范围。同时只能进行一个活动",U("Shopmanage/Event/Index"));
			die();
		}
		// 准备数据
		$data = $EVENT->prepare_data();
		$re = $EVENT->where("`sid`=".$this->sid." AND `id`=".$eid)->save($data);
		if($re){
			$this->success("修改成功！",U("Shopmanage/Edit/setting?eid=".$eid));
		}else{
			$this->success("修改失败。。。",U("Shopmanage/Edit/setting?eid=".$eid));
		}
	}
	public function setting(){
		$eid=I("eid");
		$EVENT = D("Events");
		$EVENT->eid=$eid;
		$EVENT->sid=$this->sid;
		$cfg = $EVENT->getCfg();

		$Tool = D("CheckTools");
		$Tool->sid=$this->sid;
		$Tool->where="(`sm_tools`.`tool_type`=1 OR `sm_tools`.`tool_type`=3) AND `sm_tools_status`.`status`=1 ";
		$tools = $Tool->ShowTools();
		
		$tool_code = $cfg["curtool"];
		$tool = $tools[$tool_code];
		$this->assign('eid',$eid);
		$this->assign('tool_code',$tool_code);
		$this->assign('name',$tool["name"]);
		$this->assign('pic_url',$tool["pic_url"]);
		$this->assign('tooltype',$tool["tool_type"]);
		$this->assign('tool',$tool);
		$this->assign("cfg",$cfg);
		$this->display("Event:step2");
	}
	public function gifts(){
		$eid=I("eid");
		session("lott_event_id",$eid);
		$EGIFT = D("EventsGifts");
		$EGIFT->sid=$this->sid;
		$EGIFT->eid=$eid;
		$EGIFT->getEventGifts();
		$gifts = $EGIFT->gifts;
		$gift_names = $EGIFT->gift_names;
		$gift_ids = $EGIFT->gift_ids;
		$left_prob = 10000000 - $EGIFT->sub_prob;
		$left_prob = $left_prob>0?$left_prob:0;
		
		$this->assign("range",mt_rand(100,999));
		$this->assign("left_prob",$left_prob);
		$this->assign("eid",$eid);
		$this->assign("gifts",$gifts);
		$this->display("Event:step3");
	}
	public function remove(){
		$eid = I("eid");
		$EV = M("Events");
		$re["hasError"] = !$EV->where(Array("sid"=>$this->sid,"id"=>$eid))->delete();
		$this->ajaxReturn($re);
	}
	public function show_pc_install(){
		$this->display("Event:step5_pc");
	}
	public function show_mob_install(){
		$this->display("Event:step5_mob");
	}
}